/*
 * Copyright (c) 2021 Zsj. All rights reserved.
 */

package com.jzz.crawlerweb.handler.handler;

import com.jzz.crawlerweb.handler.data.Captcha;
import com.jzz.crawlerweb.handler.data.TextCaptcha;

import java.util.Random;

public class TextCaptchaHandler extends AbstractCaptchaHandler<byte[]> {
    private static final Random random = new Random();
    private static final String[] metas = new String[]{
            "a", "b", "c", "d", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
            "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
            "2", "3", "4", "5", "6", "7", "8", "9"
    };
    private static final int length = 4;

    @Override
    public Captcha<byte[]> generate() {
        Captcha<byte[]> captcha = new TextCaptcha();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(metas[random.nextInt(metas.length)]);
        }
        captcha.setRaw(sb.toString());
        return captcha;
    }

    @Override
    public boolean verify(String input, String stored) {
        if (input == null || stored == null) {
            return false;
        }
        return input.equalsIgnoreCase(stored);
    }
}
